package com.glory.brepda.activity.technicalservice;

import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.glory.brepda.R;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.base.Constant;
import com.glory.brepda.callback.CustomDialogEntityCallBack;
import com.glory.brepda.entity.AdrefListEntity;
import com.glory.brepda.entity.EqpInfoEntity;
import com.glory.brepda.entity.EquipMentEntity;
import com.glory.brepda.entity.RepairFromEntity;
import com.glory.brepda.entity.RepairSheetDataEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.LotUtils;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DateUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class FaultRepairActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etEqpWork)
    FreeUI_ClearEditText etEqpWork;
    @BindView(R.id.etLotID)
    FreeUI_ClearEditText etLotID;
    @BindView(R.id.tvEqpLocation)
    TextView tvEqpLocation;
    @BindView(R.id.rbNormal)
    RadioButton rbNormal;
    @BindView(R.id.rbEqp)
    RadioButton rbEqp;
    @BindView(R.id.rbProgram)
    RadioButton rbProgram;
    @BindView(R.id.etFaultDesc)
    FreeUI_ClearEditText etFaultDesc;
    @BindView(R.id.tvPriority)
    TextView tvPriority;
    @BindView(R.id.ivPriorityArrow)
    ImageView ivPriorityArrow;
    @BindView(R.id.flPriorityList)
    FrameLayout flPriorityList;
    @BindView(R.id.flFaultList)
    FrameLayout flFaultList;
    @BindView(R.id.ivFaultArrow)
    ImageView ivFaultArrow;
    @BindView(R.id.tvFaultName)
    TextView tvFaultName;
    @BindView(R.id.tvOwner)
    TextView tvOwner;
    @BindView(R.id.etComment)
    FreeUI_ClearEditText etComment;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    private List<String> mPriorityList;
    private List<String> mFaultList;
    private String mRepairType = "";
    private FreeUI_GeneralPop mPriorityPop;
    private FreeUI_GeneralPop mFaultPop;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_fault_repair;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);
        tvTitle.setText("故障报修");
        mPriorityList = new ArrayList<>();
        mPriorityList.add("高");
        mPriorityList.add("中");
        mPriorityList.add("低");
        tvOwner.setText(Constant.CURRENT_USER);
        initEdit(etEqpWork, s -> {
            FreeUI_EntityCallBack<BaseResponseBean<EqpInfoEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<EqpInfoEntity>>
                    ("正在查询设备信息", new TypeToken<BaseResponseBean<EqpInfoEntity>>() {
                    }.getType(), getSupportFragmentManager(), FaultRepairActivity.this) {

                @Override
                public void onSuccess(Response<BaseResponseBean<EqpInfoEntity>> response) {
                    super.onSuccess(response);
                    if (response.body().isSuccess(FaultRepairActivity.this, true, getSupportFragmentManager())) {
                        tvEqpLocation.setText(response.body().getResponse().getBody().getEQUIPMENT().getLOCATION());
//                        requestRepairDesc(s);
                    }
                }

                @Override
                public void onError(Response<BaseResponseBean<EqpInfoEntity>> response) {
                    super.onError(response);
                    loadError(response.getException(), "BREQUERYEQP");
                }
            };

            JsonObject map = new JsonObject();
            map.addProperty("ACTIONTYPE", "EQPBYID");
            EquipMentEntity equipMentEntity = new EquipMentEntity();
            equipMentEntity.setEQUIPMENTID(s);
            map.add("EQUIPMENT", new JsonParser().parse(new Gson().toJson(equipMentEntity)).getAsJsonObject());
            NetUtils.requestPostNet("IF.BREQUERYEQP", "Common", FaultRepairActivity.this, map, callBack);

        });
    }

    private void requestRepairDesc(String s) {
        FreeUI_EntityCallBack<BaseResponseBean<AdrefListEntity>> callBack1 = new FreeUI_DialogEntityCallBack<BaseResponseBean<AdrefListEntity>>
                (new TypeToken<BaseResponseBean<AdrefListEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<AdrefListEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())){
                    AdrefListEntity body = response.body().getResponse().getBody();
                    etFaultDesc.setText(body.getALARMCODE() + "/" + body.getALARMMSG() + "/" + body.getALARMTIME());
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<AdrefListEntity>> response) {
                super.onError(response);
                loadError(response.getException(),"ADREFLISTQUERY");

            }
        };
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("EQPID",s);
        NetUtils.requestPostNet("CPC.GetEqpAlarmReport","GetEqpAlarmReport",FaultRepairActivity.this,jsonObject,callBack1);
    }

    @Override
    protected void initData() {
        FreeUI_EntityCallBack<BaseResponseBean<AdrefListEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<AdrefListEntity>>
                (new TypeToken<BaseResponseBean<AdrefListEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<AdrefListEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),true,getSupportFragmentManager())){
                    List<AdrefListEntity.Adref> adreflist = response.body().getResponse().getBody().getADREFLIST();
                    if (adreflist.size() > 0){
                        mFaultList = new ArrayList<>();
                        for (AdrefListEntity.Adref entity : adreflist)  {
                            mFaultList.add(entity.getTEXT());
                        }
                    }
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<AdrefListEntity>> response) {
                super.onError(response);
                loadError(response.getException(),"ADREFLISTQUERY");

            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("REFERENCETYPE","USER");
        map.addProperty("REFERENCENAME","BRE_EQP_REPAIR_TYPE");
        NetUtils.requestPostNet("IF.ADREFLISTQUERY","Common",FaultRepairActivity.this,map,callBack);

    }

    @OnClick({R.id.tvBaseSubmit, R.id.rbNormal, R.id.rbEqp, R.id.rbProgram, R.id.flPriorityList,R.id.flFaultList})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvBaseSubmit:
                if (mRepairType.equals("")) {
                    showToast("请选择维修类型", false);
                } else if (getText(etEqpWork).equals("")) {
                    showToast("请扫描设备编号", false);
                } else if (tvPriority.getText().toString().equals("")) {
                    showToast("请选择优先级", false);
                } else {
                    showSubmitDialog(() -> submit());
                }
                break;
            case R.id.rbNormal:
                mRepairType = "normal";
                break;
            case R.id.rbEqp:
                mRepairType = "eqp";
                break;
            case R.id.rbProgram:
                mRepairType = "program";
                break;
            case R.id.flPriorityList:
                if (mPriorityPop != null) {
                    if (mPriorityPop.isShowing()) {
                        mPriorityPop.dismiss();
                    }
                } else {
                    mPriorityPop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flPriorityList.getWidth(), mPriorityList, ivPriorityArrow, (codeS, i) -> {
                        tvPriority.setText(codeS);
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivPriorityArrow);
                mPriorityPop.showAsDropDown(flPriorityList, 0, 0);
                break;
            case R.id.flFaultList:
                if (mFaultList == null || mFaultList.size() == 0){
                    showToast("未获取到故障名", false);
                    return;
                }
                if (mFaultPop != null) {
                    if (mFaultPop.isShowing()) {
                        mFaultPop.dismiss();
                    }
                } else {
                    mFaultPop = FreeApi_PopUtils.getNormalPop(getApplicationContext(), flFaultList.getWidth(), mFaultList, ivFaultArrow, (codeS, i) -> {
                        tvFaultName.setText(codeS);
                    });
                }
                FreeApi_PopUtils.setArrowShow(ivFaultArrow);
                mFaultPop.showAsDropDown(flFaultList, 0, 0);
                break;
        }
    }

    private void submit() {
        if (getText(etFaultDesc).equals("")){
            showToast("请输入故障描述",false);
            return;
        }
        FreeUI_EntityCallBack<BaseResponseBean<JSONObject>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<JSONObject>>
                (new TypeToken<BaseResponseBean<JSONObject>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<JSONObject>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(),false,getSupportFragmentManager())){
                        showConfirmDialog(FaultRepairActivity.this,"提交成功");
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<JSONObject>> response) {
                super.onError(response);
                loadError(response.getException(),"PMSREPAIR");

            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE","START");
        map.addProperty("REPAIRTYPE",mRepairType);
        RepairFromEntity repairFromEntity = new RepairFromEntity(Constant.CURRENT_USER,getText(etEqpWork));
        repairFromEntity.setCOMMENTS(getText(etComment));
        map.add("REPAIRFORM",new JsonParser().parse(new Gson().toJson(repairFromEntity)).getAsJsonObject());
        RepairSheetDataEntity repairSheetDataEntity = new RepairSheetDataEntity();
        repairSheetDataEntity.setEQUIPMENTID(getText(etEqpWork));
        repairSheetDataEntity.setPRIORITY(tvPriority.getText().toString());
        repairSheetDataEntity.setLOTID(getText(etLotID));
        //SHIJIAN
        repairSheetDataEntity.setTROUBLETIME(FreeApi_DateUtils.getNowStringDate("yyyy/MM/dd HH:mm:ss"));
        repairSheetDataEntity.setTROUBLEREPORTER(Constant.CURRENT_USER);
        repairSheetDataEntity.setTROUBLECOMMENT(etFaultDesc.getText().toString());
        repairSheetDataEntity.setOWNER(Constant.CURRENT_USER);
        repairSheetDataEntity.setRESERVED1(tvFaultName.getText().toString());
        map.add("REPAIRSHEETDATA",new JsonParser().parse(new Gson().toJson(repairSheetDataEntity)).getAsJsonObject());
        NetUtils.requestPostNet("IF.PMSREPAIR","PMSREPAIR",FaultRepairActivity.this,map,callBack);
    }

}
